package com.ny.dao.impl;

import com.ny.dao.AdminDao;
import com.ny.entity.Admin;
import com.ny.entity.Order;
import com.ny.entity.Product;
import com.ny.entity.Student;
import com.ny.until.DbUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public abstract class AdminDaoImpl implements AdminDao {
    private DbUtil dbUtil = new DbUtil();

    @Override
    public Admin selectAdmin(String username, String password) throws SQLException {
        Connection connection = dbUtil.getCon();
        String sql = "SELECT * FROM managers WHERE name = ? AND password = ?";
        PreparedStatement prepareStatement = connection.prepareStatement(sql);
        prepareStatement.setString(1, username);
        prepareStatement.setString(2, password);
        ResultSet resultSet = prepareStatement.executeQuery();

        if (resultSet.next()) {
            Admin admin = new Admin();
            admin.setAdminId(resultSet.getInt("manager_id"));
            admin.setUsername(resultSet.getString("name"));
            admin.setPassword(resultSet.getString("password"));
            return admin;
        }
        return null;
    }
    public List<Student> selectAllStudents() throws SQLException {
        Connection connection = dbUtil.getCon();
        String sql = "SELECT * FROM students";
        PreparedStatement prepareStatement = connection.prepareStatement(sql);
        ResultSet resultSet = prepareStatement.executeQuery();

        List<Student> students = new ArrayList<>();
        while (resultSet.next()) {
            Student student = new Student();
            student.setStudentId(resultSet.getInt("student_id"));
            student.setName(resultSet.getString("name"));
            student.setPhone(resultSet.getString("phone"));
            student.setPassword(resultSet.getString("password"));
            students.add(student);
        }
        return students;
    }
    public int insertStudent(Student student) throws SQLException {
        Connection connection = dbUtil.getCon();
        String sql = "INSERT INTO students(name, phone, password) VALUES (?, ?, ?)";
        PreparedStatement prepareStatement = connection.prepareStatement(sql);
        prepareStatement.setString(1, student.getName());
        prepareStatement.setString(2, student.getPhone());
        prepareStatement.setString(3, student.getPassword());
        return prepareStatement.executeUpdate();
    }


    public int updateStudent(Student student) throws SQLException {
        Connection connection = dbUtil.getCon();
        String sql = "UPDATE students SET name = ?, phone = ?, password = ? WHERE student_id = ?";
        PreparedStatement prepareStatement = connection.prepareStatement(sql);
        prepareStatement.setString(1, student.getName());
        prepareStatement.setString(2, student.getPhone());
        prepareStatement.setString(3, student.getPassword());
        prepareStatement.setInt(4, student.getStudentId());
        return prepareStatement.executeUpdate();
    }


    public int deleteStudent(int studentId) throws SQLException {
        Connection connection = dbUtil.getCon();
        String sql = "DELETE FROM students WHERE student_id = ?";
        PreparedStatement prepareStatement = connection.prepareStatement(sql);
        prepareStatement.setInt(1, studentId);
        return prepareStatement.executeUpdate();
    }


    public int insertProduct(Product product) throws SQLException {
        Connection connection = dbUtil.getCon();
        String sql = "INSERT INTO products(name, price, stock, description) VALUES (?, ?, ?, ?)";
        PreparedStatement prepareStatement = connection.prepareStatement(sql);
        prepareStatement.setString(1, product.getName());
        prepareStatement.setDouble(2, product.getPrice());
        prepareStatement.setInt(3, product.getStock());
        prepareStatement.setString(4, product.getDescription());
        return prepareStatement.executeUpdate();
    }


    public int updateProduct(Product product) throws SQLException {
        Connection connection = dbUtil.getCon();
        String sql = "UPDATE products SET name = ?, price = ?, stock = ?, description = ? WHERE product_id = ?";
        PreparedStatement prepareStatement = connection.prepareStatement(sql);
        prepareStatement.setString(1, product.getName());
        prepareStatement.setDouble(2, product.getPrice());
        prepareStatement.setInt(3, product.getStock());
        prepareStatement.setString(4, product.getDescription());
        prepareStatement.setInt(5, product.getProductId());
        return prepareStatement.executeUpdate();
    }


    public int deleteProduct(int productId) throws SQLException {
        Connection connection = dbUtil.getCon();
        String sql = "DELETE FROM products WHERE product_id = ?";
        PreparedStatement prepareStatement = connection.prepareStatement(sql);
        prepareStatement.setInt(1, productId);
        return prepareStatement.executeUpdate();
    }


    public List<Order> selectAllOrders() throws SQLException {
        Connection connection = dbUtil.getCon();
        String sql = "SELECT * FROM orders";
        PreparedStatement prepareStatement = connection.prepareStatement(sql);
        ResultSet resultSet = prepareStatement.executeQuery();

        List<Order> orders = new ArrayList<>();
        while (resultSet.next()) {
            Order order = new Order();
            order.setOrderId(resultSet.getInt("order_id"));
            order.setStudentId(resultSet.getInt("student_id"));
            order.setProductId(resultSet.getInt("product_id"));
            order.setNumber(resultSet.getInt("number"));
            order.setTotalPrice(resultSet.getDouble("total_price"));
            orders.add(order);
        }
        return orders;
    }


    public int deleteOrder(int orderId) throws SQLException {
        Connection connection = dbUtil.getCon();
        String sql = "DELETE FROM orders WHERE order_id = ?";
        PreparedStatement prepareStatement = connection.prepareStatement(sql);
        prepareStatement.setInt(1, orderId);
        return prepareStatement.executeUpdate();
    }
}